define(function (require, exports, module) {
    var main = require("apps/process/main");
    exports.init = function (ctx, uuid) {
        var page = new $.PageDomFactory(uuid);
        $.getJSON(ctx + "/app/api/directory/list", {dirGroup: "DIRECTORY_PROCESS_CONFIG"}).done(function (data) {
            var content = data.content;
            page.getJqDom("moduleId").combobox({
                valueField: 'id',
                textField: 'dirValue',
                editable: false,
                data: content
            });
        });
        $.getJSON(ctx + "/app/api/directory/list", {dirGroup: "DIRECTORY_PROCESS_UNIT"}).done(function (data) {
            var content = data.content;
            content.unshift({id: "", dirValue: "无"});
            var options = content.map(function (item) {
                return "<option value=\"" + item.dirValue + "\">" + item.dirValue + "</option>";
            });
            page.getJqDom("unit").html(options.join(""));
            page.getJqDom("add-attr").linkbutton("enable");
        });

        require("apps/easyui");
        page.getJqDom("useAble").toggleButton({
            dataRef: {
                "0": "停用",
                "1": "启用"
            }
        });
        page.getJqDom("useDevice").toggleButton({
            dataRef: {
                "0": "不使用",
                "1": "使用"
            },
            onCheck: function (value) {
                if (value == "1") {
                    var de = page.getJqDom("device").show();
                    if (de.attr("data-ref") != "1") {
                        de.attr("data-ref", "1");
                        page.getJqDom("deviceId").combobox({
                            valueField: 'id',
                            textField: 'deviceName',
                            multiple: false,
                            url: ctx + "/app/api/device/list?parserDevice=1",
                            mode: "remote",
                            formatter: function (row) {
                                var opts = $(this).combobox('options');
                                var img = "<img src=\"" + WE.contextPath + "/print/" + row.deviceCover + "\" style=\"width:48px;height:32px;margin-right:10px;\"/>"
                                return img + row[opts.textField] + "(" + row.deviceCode + ")";
                            },
                            loader: function (param, success, error) {
                                var opts = $(this).combobox('options');
                                if (!opts.url) return false;
                                $.ajax({
                                    type: opts.method,
                                    url: opts.url,
                                    data: param,
                                    dataType: 'json',
                                    success: function (data) {
                                        success(data.content);
                                    },
                                    error: function () {
                                        error.apply(this, arguments);
                                    }
                                });
                            }
                        });
                    }
                } else {
                    page.getJqDom("device").hide();
                }
            }
        });

        page.getJqDom("add-attr").linkbutton({
            disabled: true,
            onClick: function () {
                var dom = page.getJqDom("hidden-template").find("tbody").html();
                $(dom).appendTo(page.getJqDom("table-attr")).find("[data-id=del]").linkbutton({
                    onClick: function () {
                        $(this).closest("tr.hydra-tr-bg").remove();
                    }
                }).end().find("select").each(function () {
                    var dataId = $(this).attr("data-id");
                    if (dataId == "fieldType") {
                        $(this).combobox({
                            editable: false,
                            onSelect: function (record) {
                                var c = record.value;
                                var dir = $(this).closest("tr").find("[data-id=directory]"),
                                    normal = $(this).closest("tr").find("[data-id=normal]"),
                                    table = $(this).closest("tr").find("[data-id=table]");
                                if (c == 'FIELD_TYPE_DIRECTORY') {
                                    dir.show();
                                    normal.hide();
                                    table.hide();
                                } else if(c == 'FIELD_TYPE_TABLE'){
                                    dir.hide();
                                    table.show();
                                    normal.hide();
                                }else{
                                    dir.hide();
                                    table.hide();
                                    normal.show();
                                }
                            }
                        })
                    } else {
                        $(this).combobox({
                            editable: false
                        })
                    }
                }).end().find(".easyui-validatebox").validatebox();
                main.initSort();
            }
        });
        main.init(ctx, uuid, {
            postUrl: ctx + "/app/api/process/add"
        });
    }
});